package com.woniu.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ProjectMapper extends BaseMapper<Project> {

        @Insert("insert into project(bank_id,user_id,pro_name,contract,pro_start_time,pro_end_time,pre_price,pro_status) values(#{bank.bankId},#{user.userId},#{proName},#{contract},#{proStartTime},#{proEndTime},#{prePrice},#{proStatus})")
        @Options(useGeneratedKeys = true,keyColumn ="pro_id",keyProperty ="proId")
        void add(Project project);


        @Select("select * from project")
        List<Project>getAll();

//多表联查???

        @Select("select * from project where pro_id=#{proId}")//p,pro_approve pa,pro_purchase pp,pro_spence ps ,user_mid_pro ump p.pro_id=pa.pro_id=pp.pro_id=ps.pro_id=ump.pro_id and
        @Results(id="projectMap",value={
                @Result(column = "pro_id",property = "proId",id = true),
                @Result(column = "bank_id",property = "bank.bankId"),
                @Result(column = "user_id",property = "user.userId"),
                @Result(column = "pro_name",property = "proName"),
                @Result(column = "contract",property = "contract"),
                @Result(column = "pro_start_time",property = "proStartTime"),
                @Result(column = "pro_end_time",property = "proEndTime"),
                @Result(column = "pre_price",property = "prePrice"),
                //@Result(column = "left_price",property = "leftPrice"),
                @Result(column = "pro_status",property = "proStatus"),
                @Result(column="users",many=@Many(select ="com.woniuxy.UserMapper.getUserByProId",fetchType = FetchType.LAZY)),//hyl
                @Result(column="proApproves",many=@Many(select ="com.woniuxy.ProApprovesMapper.getProApproveByProId",fetchType = FetchType.LAZY)),
                @Result(column="proPurchases",many=@Many(select ="com.woniuxy.ProPurchasesMapper.getByProId",fetchType = FetchType.LAZY)),//zw
                @Result(column="proSpences",many=@Many(select ="com.woniuxy.ProSpencesMapper.selectProSpences",fetchType = FetchType.LAZY)),//fzn
        })
        Project getByProId(int proId);


        @Select("select count(*) from project p where pro_status=#{proStatus} and pro_id=#{proId}")
        @ResultMap("projectMap")
        Integer getNumsByProStatusAndProId(Project project);

        @Select("select * from project p where pro_status=#{proStatus} and pro_id=#{proId}")
        @ResultMap("projectMap")
        List<Project> getProjectByProStatusAndProId(Project project);


        @SelectProvider(type = ProjectSqlProvider.class, method = "select")
        List<Project> getByCondition(Project project);

        @UpdateProvider(type = ProjectSqlProvider.class, method = "update")
        void update(Project project);

    //创建项目
    @Insert("insert into project values (#{proId},#{bank.bankId},#{user.userId},#{proName},#{contract},#{proStartTime},#{proEndTime},#{prePrice},'r')")
    void insertPro(Project project);

    //重新提交,更改信息
    @Update("update project set bank_id = #{bank.bankId}, user_id = #{user.userId}, pro_name = #{proName}, contract = #{contract}, pro_start_time = #{proStartTime}, pre_price = #{prePrice}, pro_status = 'r' where pro_id = #{proId}")
    void reSubmit(Project project);


    //审批通过
    @Update("update project set pro_status = 'y' where pro_id = #{proid}")
    void ppass(int proid);

    //审核未通过
    @Update("update project set pro_status = 'n' where pro_id = #{proid}")
    void punpass(int proid);

    //项目结束
    //表里应该添加一个字段用来记录实际项目结束时间（待商榷）
    @Update("update project set pro_real_end_time = #{endtime}pro_status = 'e' where pro_id = #{proid}")
    void endpro(@Param("proid") int proid,@Param("endtime") String endtime);

        @Slf4j
        public  class ProjectSqlProvider {
            public static String update(Project project) {

                return new SQL() {
                    {
                        UPDATE("project");
                        if(project.getBank()!=null&&!"".equals(project.getBank().toString().trim())){//这样是判断给对象开辟了空间但没有值的情况
                            SET("bank_id=#{bankId}");
                        }
                        if(project.getUser()!=null&&!"".equals(project.getUser().toString().trim())){
                            SET("user_id=#{userId}");
                        }
                        if(project.getProName()!=null&&!"".equals(project.getProName().toString().trim())){
                            SET("pro_name=#{proName}");
                        }
                        if(project.getContract()!=null&&!"".equals(project.getContract().toString().trim())){
                            SET("contract=#{contract}");
                        }
                        if(project.getProStartTime()!=null&&!"".equals(project.getProStartTime().toString().trim())){
                            SET("pro_start_time=#{proStartTime}");
                        }
                        if(project.getProEndTime()!=null&&!"".equals(project.getProEndTime().toString().trim())){
                            SET("pro_end_time=#{proEndTime}");
                        }
                        if(project.getPrePrice()!=null&&!"".equals(project.getPrePrice().toString().trim())){
                            SET("pre_price=#{prePrice}");
                        }
//                        if(project.getLeftPrice()!=null&&!"".equals(project.getLeftPrice().toString().trim())){
//                            SET("left_price=#{leftPrice}");
//                        }
                        if(project.getProStatus()!=null&&!"".equals(project.getProStatus().toString().trim())){
                            SET("pro_status=#{proStatus}");
                        }
                        WHERE("pro_id=#{proId}");
                    }
                }.toString();
                //log.debug("SQL:{}",sql);
            }

            public static String select(Project project) {
                return new SQL() {{
                    SELECT("*");
                    FROM("project");
                    if (project.getProId() != 0) {
                        WHERE("pro_id=#{proId}");
                    }
                    if (project.getBank() != null&&!"".equals(project.getBank().toString().trim())) {
                        WHERE("bank_id = #{bankId}");
                    }
                    if (project.getUser() != null&&!"".equals(project.getUser().toString().trim())) {
                        WHERE("user_id=#{userId}");
                    }
                    if (project.getProName() != null&&!"".equals(project.getProName().toString().trim())) {
                        WHERE("pro_name=#{proName}");
                    }
                    if (project.getContract() != null&&!"".equals(project.getContract().toString().trim())) {
                        WHERE("contract=#{contract}");
                    }
                    if (project.getProStartTime() != null&&!"".equals(project.getProStartTime().toString().trim())) {
                        WHERE("pro_start_time=#{proStartTime}");
                    }
                    if (project.getProEndTime() != null&&!"".equals(project.getProEndTime().toString().trim())) {
                        WHERE("pro_end_time=#{proEndTime}");
                    }
                    if (project.getPrePrice() != null&&!"".equals(project.getPrePrice().toString().trim())) {
                        WHERE("pre_price=#{prePrice}");
                    }
//                    if (project.getLeftPrice() != null&&!"".equals(project.getLeftPrice().toString().trim())) {
//                        WHERE("left_price=#{leftPrice}");
//                    }
                    if (project.getProStatus() != null&&!"".equals(project.getProStatus().toString().trim())) {
                        WHERE("pro_status=#{proStatus}");
                    }
                    if (project.getStartTime() != null&&!"".equals(project.getStartTime().toString().trim())) {
                        WHERE("pro_start_time &gt;=#{startTime}");
                    }
                    if (project.getEndTime() != null&&!"".equals(project.getEndTime().toString().trim())) {
                        WHERE("pro_end_time &lt;=#{endTime}");
                    }
                    if (project.getMinPrePrice() != null&&!"".equals(project.getMinPrePrice().toString().trim())) {
                        WHERE("pre_price &gt;=#{minPrePrice}");
                    }
                    if (project.getMaxPrePrice() != null&&!"".equals(project.getMaxPrePrice().toString().trim())) {
                        WHERE("pre_price &lt;=#{maxPrePrice}");
                    }
//                    if (project.getMinLeftPrice() != null&&!"".equals(project.getMinLeftPrice().toString().trim())) {
//                        WHERE("left_price &gt;=#{minLeftPrice}");
//                    }
//                    if (project.getMaxLeftPrice() != null&&!"".equals(project.getMaxLeftPrice().toString().trim())) {
//                        WHERE("left_price &lt;=#{maxLeftPrice}");
//                    }
                    ORDER_BY("pro_id");
                }}.toString();
            }
        }
}
